package queryfish.antlr.parser;

import java.util.ArrayList;
import java.util.StringTokenizer;

public class analysisQues {
	ParserQues parse = new ParserQues();
	public String kindQues = "";
	public static String keyworld = "";
	public static String nameFish = "";
	public String nameFish1 = "";
	String s1 = "";
	String s2 = "";

	Boolean verbPharseStartQues = false;
	Boolean iN = false;
	Boolean expletive = false;
	Boolean vbBe = false;
	Boolean nnFish = false;
	int countNnfish = 0;
	Boolean advTo = false;
	Boolean dtThe = false;
	Boolean inCan = false;
	Boolean vbNeed = false;

	ArrayList<String> phrases = new ArrayList<>();

	private void kinhOfQues(String question) {
		StringTokenizer st = new StringTokenizer(question, ")(?");
		while (st.hasMoreTokens())
			phrases.add(st.nextToken());
		kindQues = phrases.get(1);
		for (int i = 0; i < phrases.size(); i++) {
			if (phrases.get(i).equals(" ")) {
				phrases.remove(i);
			}
		}
	}

	private void wordHide(String question) {
		parse.quest("verbPharseStartQues", question);
		verbPharseStartQues = parse.s;
		parse.quest("iN", question);
		iN = parse.s;
		parse.quest("expletive03", question);
		expletive = parse.s;
		parse.quest("expletive07", question);
		expletive = parse.s;
		parse.quest("vbBe", question);
		vbBe = parse.s;
		parse.quest("nnFish", question);
		nnFish = parse.s;
		countNnfish = parse.count;
		parse.quest("advTo", question);
		advTo = parse.s;
		parse.quest("dtThe", question);
		dtThe = parse.s;
		parse.quest("inCan", question);
		inCan = parse.s;
		parse.quest("vbNeed", question);
		vbNeed = parse.s;
	}

	public void splitWord(String s) {
		s1 = "";
		s2 = "";
		ArrayList<String> temp = new ArrayList<>();
		StringTokenizer st = new StringTokenizer(s, " ");
		while (st.hasMoreTokens())
			temp.add(st.nextToken());
		for (int i = 0; i < temp.size(); i++) {
			s1 = temp.get(0);
			if (i > 0) {
				s2 += temp.get(i) + " ";
			}
		}
	}

	public void takeKey(String question) {
		kinhOfQues(question);
		wordHide(question);
		// System.out.print("\n size: " + phrases.size());
		// questionWh01 quetionWh02 questionWh09
		// for (int i = 0; i < phrases.size(); i++)
		// {
		// // System.out.print("\n" + i + " --" + phrases.get(i));
		// }
		// System.out.print("\n ques: " + kindQues);
		if ((kindQues.equals("questionWh01 ") == true)
				|| (kindQues.equals("questionWh02 ") == true)
				|| (kindQues.equals("questionWh09 ") == true)) {
			if ((verbPharseStartQues == true) && (iN == true)) {
				if (phrases.get(3).equals("info01 ")
						|| phrases.get(3).equals("info02 ")) {
					if (phrases.get(4).equals("name ")
							|| phrases.get(4).equals("request ")
							|| phrases.get(4).equals("waterRequest ")) {
						splitWord(phrases.get(5));
						keyworld = s1;
						splitWord(phrases.get(8));
						nameFish = s2;
					}

					else {
						splitWord(phrases.get(4));
						keyworld = s1;
						splitWord(phrases.get(7));
						nameFish = s2;
					}
				} else {
					splitWord(phrases.get(3));
					keyworld = s1;
					splitWord(phrases.get(6));
					nameFish = s2;
				}
			}
			if ((verbPharseStartQues == true) && (iN == false)) {
				if (phrases.get(3).equals("info01 ")
						|| phrases.get(3).equals("info02 ")) {
					if (phrases.get(4).equals("name ")
							|| phrases.get(4).equals("request ")
							|| phrases.get(4).equals("waterRequest ")) {
						splitWord(phrases.get(5));
						keyworld = s1;
						splitWord(phrases.get(7));
						nameFish = s2;
					} else {
						splitWord(phrases.get(4));
						keyworld = s1;
						splitWord(phrases.get(6));
						nameFish = s2;
					}
				} else {
					splitWord(phrases.get(3));
					keyworld = s1;
					splitWord(phrases.get(5));
					nameFish = s2;
				}
			}
			if ((verbPharseStartQues == false) && (iN == true)) {
				if (phrases.get(2).equals("info01 ")
						|| phrases.get(2).equals("info02 ")) {
					if (phrases.get(3).equals("name ")
							|| phrases.get(3).equals("request ")
							|| phrases.get(3).equals("waterRequest ")) {
						splitWord(phrases.get(4));
						keyworld = s1;
						splitWord(phrases.get(7));
						nameFish = s2;
					} else {
						splitWord(phrases.get(3));
						keyworld = s1;
						splitWord(phrases.get(6));
						nameFish = s2;
					}
				} else {
					splitWord(phrases.get(2));
					keyworld = s1;
					splitWord(phrases.get(5));
					nameFish = s2;
				}
			}
			if ((verbPharseStartQues == false) && (iN == false)) {
				if (phrases.get(2).equals("info01 ")
						|| phrases.get(2).equals("info02 ")) {
					if (phrases.get(3).equals("name ")
							|| phrases.get(3).equals("request ")
							|| phrases.get(3).equals("waterRequest ")) {
						splitWord(phrases.get(4));
						keyworld = s1;
						splitWord(phrases.get(6));
						nameFish = s2;
					} else {
						splitWord(phrases.get(3));
						keyworld = s1;
						splitWord(phrases.get(5));
						nameFish = s2;
					}
				} else {
					splitWord(phrases.get(2));
					keyworld = s1;
					splitWord(phrases.get(4));
					nameFish = s2;
				}
			}
		}
		// questionWh03: "cho biet ca A la ca nao?"
		if (kindQues.equals("questionWh03 ")) {
			if (verbPharseStartQues == true) {
				splitWord(phrases.get(4));
				nameFish = s2;
			} else {
				splitWord(phrases.get(3));
				nameFish = s2;
			}
		}
		// questionWh04: // ca A co B la?
		if (kindQues.equals("questionWh04 ")) {
			if (verbPharseStartQues == true) {
				if (phrases.get(7).equals("name ")
						|| phrases.get(7).equals("request ")
						|| phrases.get(7).equals("waterRequest ")) {
					splitWord(phrases.get(8));
					keyworld = s1;
					splitWord(phrases.get(4));
					nameFish = s2;
				} else {
					splitWord(phrases.get(7));
					keyworld = s1;
					splitWord(phrases.get(4));
					nameFish = s2;
				}
			} else {
				if (phrases.get(6).equals("name ")
						|| phrases.get(6).equals("request ")
						|| phrases.get(6).equals("waterRequest ")) {
					splitWord(phrases.get(7));
					keyworld = s1;
					splitWord(phrases.get(3));
					nameFish = s2;
				} else {
					splitWord(phrases.get(6));
					keyworld = s1;
					splitWord(phrases.get(3));
					nameFish = s2;
				}
			}
		}
		// questionWh05 (cho biết) cá (nào) có tên khoa học (là) A?
		if (kindQues.equals("questionWh05 ")) {
			if (verbPharseStartQues == true && expletive == true
					&& vbBe == true) {
				splitWord(phrases.get(7));
				keyworld = s1;
				splitWord(phrases.get(9));
				nameFish1 = s2;
			}
			if (((verbPharseStartQues == false && expletive == true) || (verbPharseStartQues == true && expletive == false))
					&& (vbBe == true)) {
				splitWord(phrases.get(6));
				keyworld = s1;
				splitWord(phrases.get(8));
				nameFish1 = s2;
			}
			if (verbPharseStartQues == true && expletive == true
					&& vbBe == false) {
				splitWord(phrases.get(7));
				keyworld = s1;
				splitWord(phrases.get(8));
				nameFish1 = s2;
			}
			if (((verbPharseStartQues == false && expletive == true) || (verbPharseStartQues == true && expletive == false))
					&& (vbBe == false)) {
				splitWord(phrases.get(6));
				keyworld = s1;
				splitWord(phrases.get(7));
				nameFish1 = s2;
			}
			if (verbPharseStartQues == false && expletive == false
					&& vbBe == true) {
				splitWord(phrases.get(5));
				keyworld = s1;
				splitWord(phrases.get(7));
				nameFish1 = s2;
			}
			if (verbPharseStartQues == false && expletive == false
					&& vbBe == false) {
				splitWord(phrases.get(5));
				keyworld = s1;
				splitWord(phrases.get(6));
				nameFish1 = s2;
			}
		}
		// questionWh06: (cho biết) (cá) A là tên khoa học của cá nào?
		if (kindQues.equals("questionWh06 ")) {
			if (verbPharseStartQues == true && countNnfish == 2) {
				splitWord(phrases.get(7));
				keyworld = s1;
				splitWord(phrases.get(4));
				nameFish1 = s2;
			}
			if ((verbPharseStartQues == true && countNnfish == 1)
					|| (verbPharseStartQues == false && countNnfish == 2)) {
				splitWord(phrases.get(6));
				keyworld = s1;
				splitWord(phrases.get(3));
				nameFish1 = s2;
			}
			if (verbPharseStartQues == false && countNnfish == 1) {
				splitWord(phrases.get(5));
				keyworld = s1;
				splitWord(phrases.get(2));
				nameFish1 = s2;
			}
		}
		// questionWh07: (cho biết) cá A có kỹ thuật nuôi như thế nào?
		if (kindQues.equals("questionWh07 ")) {
			if (verbPharseStartQues == true) {
				if (phrases.get(7).equals("name ")
						|| phrases.get(7).equals("request ")
						|| phrases.get(7).equals("waterRequest ")) {
					splitWord(phrases.get(8));
					keyworld = s1;
					splitWord(phrases.get(4));
					nameFish = s2;
				} else {
					splitWord(phrases.get(7));
					keyworld = s1;
					splitWord(phrases.get(4));
					nameFish = s2;
				}
			} else {
				if (phrases.get(6).equals("name ")
						|| phrases.get(6).equals("request ")
						|| phrases.get(6).equals("waterRequest ")) {
					splitWord(phrases.get(7));
					keyworld = s1;
					splitWord(phrases.get(3));
					nameFish = s2;
				} else {
					splitWord(phrases.get(6));
					keyworld = s1;
					splitWord(phrases.get(3));
					nameFish = s2;
				}
			}
		}
		// questionWh08: cho biết cá A thuộc bộ cá nào?
		if (kindQues.equals("questionWh08 ")) {
			if (verbPharseStartQues == true) {
				splitWord(phrases.get(7));
				keyworld = s1;
				splitWord(phrases.get(4));
				nameFish = s2;
			} else {
				splitWord(phrases.get(6));
				keyworld = s1;
				splitWord(phrases.get(3));
				nameFish = s2;
			}
		}
		// questionWh10 questionWh11
		if ((kindQues.equals("questionWh10 ") == true)
				|| (kindQues.equals("questionWh11 ") == true)) {
			if (verbPharseStartQues == true) {
				splitWord(phrases.get(6));
				keyworld = s1;
				splitWord(phrases.get(4));
				nameFish = s2;
			} else {
				splitWord(phrases.get(5));
				keyworld = s1;
				splitWord(phrases.get(3));
				nameFish = s2;
			}
		}
		// questionWh12 cho biết cá A có độ dH là bao nhiêu?
		if (kindQues.equals("questionWh12 ")) {
			if (verbPharseStartQues == true) {
				if (phrases.get(6).equals("waterRequest ")) {
					splitWord(phrases.get(7));
					keyworld = s1;
					splitWord(phrases.get(4));
					nameFish = s2;
				} else {
					splitWord(phrases.get(6));
					keyworld = s1;
					splitWord(phrases.get(4));
					nameFish = s2;
				}
			} else {
				if (phrases.get(5).equals("waterRequest ")) {
					splitWord(phrases.get(6));
					keyworld = s1;
					splitWord(phrases.get(3));
					nameFish = s2;
				} else {
					splitWord(phrases.get(5));
					keyworld = s1;
					splitWord(phrases.get(3));
					nameFish = s2;
				}
			}
		}
		// questionWh13 cho biết cá A sống trong nhiệt độ nước là bao nhiêu?
		if (kindQues.equals("questionWh13 ")) {
			if (verbPharseStartQues == true) {
				splitWord(phrases.get(8));
				keyworld = s1;
				splitWord(phrases.get(4));
				nameFish = s2;
			} else {
				splitWord(phrases.get(7));
				keyworld = s1;
				splitWord(phrases.get(3));
				nameFish = s2;
			}
		}
		// questionWh14: (cho biết) nhiệt độ nước (để) nuôi cá A là bao nhiêu?
		if (kindQues.equals("questionWh14 ")) {
			if (verbPharseStartQues == true && advTo == true) {
				splitWord(phrases.get(4));
				keyworld = s1;
				splitWord(phrases.get(8));
				nameFish = s2;
			}
			if (verbPharseStartQues == true && advTo == false) {
				splitWord(phrases.get(4));
				keyworld = s1;
				splitWord(phrases.get(7));
				nameFish = s2;
			}
			if (verbPharseStartQues == false && advTo == true) {
				splitWord(phrases.get(3));
				keyworld = s1;
				splitWord(phrases.get(7));
				nameFish = s2;
			}
			if (verbPharseStartQues == false && advTo == false) {
				splitWord(phrases.get(3));
				keyworld = s1;
				splitWord(phrases.get(6));
				nameFish = s2;
			}
		}
		// questionWh15: (cho biết) cá A sống trong nước có nhiệt độ nước là bao
		// nhiêu?
		if (kindQues.equals("questionWh15 ")) {
			if (verbPharseStartQues == true) {
				splitWord(phrases.get(10));
				keyworld = s1;
				splitWord(phrases.get(4));
				nameFish = s2;
			} else {
				splitWord(phrases.get(9));
				keyworld = s1;
				splitWord(phrases.get(3));
				nameFish = s2;
			}
		}
		// questionWh16: (cho biết) cá A được chăm sóc thế nào?
		if (kindQues.equals("questionWh16 ")) {
			if (verbPharseStartQues == true) {
				if (phrases.get(6).equals("request ")) {
					splitWord(phrases.get(7));
					keyworld = s1;
					splitWord(phrases.get(4));
					nameFish = s2;
				} else {
					splitWord(phrases.get(6));
					keyworld = s1;
					splitWord(phrases.get(4));
					nameFish = s2;
				}
			} else {

				if (phrases.get(5).equals("request ")) {
					splitWord(phrases.get(6));
					keyworld = s1;
					splitWord(phrases.get(3));
					nameFish = s2;
				} else {
					splitWord(phrases.get(5));
					keyworld = s1;
					splitWord(phrases.get(3));
					nameFish = s2;
				}
			}
		}
		// questionWh17: (cho biết) bể nuôi (của) cá A có thiết kế như thế nào?
		if (kindQues.equals("questionWh17 ")) {
			if (verbPharseStartQues == true && iN == true) {
				if (phrases.get(9).equals("request ")
						|| phrases.get(9).equals("waterRequest ")) {
					splitWord(phrases.get(10));
					keyworld = s1;
					splitWord(phrases.get(7));
					nameFish = s2;
				} else {
					splitWord(phrases.get(9));
					keyworld = s1;
					splitWord(phrases.get(7));
					nameFish = s2;
				}
			}
			if ((verbPharseStartQues == false && iN == true)
					|| (verbPharseStartQues == true && iN == false)) {

				if (phrases.get(8).equals("request ")
						|| phrases.get(8).equals("waterRequest ")) {
					splitWord(phrases.get(9));
					keyworld = s1;
					splitWord(phrases.get(6));
					nameFish = s2;
				} else {
					splitWord(phrases.get(8));
					keyworld = s1;
					splitWord(phrases.get(6));
					nameFish = s2;
				}
			}
			if (verbPharseStartQues == false && iN == false) {

				if (phrases.get(7).equals("request ")
						|| phrases.get(7).equals("waterRequest ")) {
					splitWord(phrases.get(8));
					keyworld = s1;
					splitWord(phrases.get(5));
					nameFish = s2;
				} else {
					splitWord(phrases.get(7));
					keyworld = s1;
					splitWord(phrases.get(5));
					nameFish = s2;
				}
			}
		}
		// questionWh18: (cho biết) (những) loại thức ăn (nào) (có thể) cho cá A
		// ăn được?
		if (kindQues.equals("questionWh18 ")) {
			keyworld = "kindOfFood";
			int a = 0;
			a = count(a);
			splitWord(phrases.get(9 - a));
			nameFish = s2;

		}
		// questionWh19: (cho biết) cá A (cần) (được) chăm sóc như thế nào?
		if (kindQues.equals("questionWh19 ")) {
			if (verbPharseStartQues == true && vbNeed == true
					&& expletive == true) {
				splitWord(phrases.get(7));
				keyworld = s1;
				splitWord(phrases.get(4));
				nameFish = s2;
			}
			if (verbPharseStartQues == true
					&& ((vbNeed == true && expletive == false) || (vbNeed == false && expletive == true))) {
				splitWord(phrases.get(6));
				keyworld = s1;
				splitWord(phrases.get(4));
				nameFish = s2;
			}
			if (verbPharseStartQues == false
					&& ((vbNeed == true && expletive == false) || (vbNeed == false && expletive == true))) {
				splitWord(phrases.get(5));
				keyworld = s1;
				splitWord(phrases.get(3));
				nameFish = s2;
			}
			if (verbPharseStartQues == true && vbNeed == false
					&& expletive == false) {
				splitWord(phrases.get(5));
				keyworld = s1;
				splitWord(phrases.get(4));
				nameFish = s2;
			}
			if (verbPharseStartQues == false && vbNeed == true
					&& expletive == true) {
				splitWord(phrases.get(6));
				keyworld = s1;
				splitWord(phrases.get(3));
				nameFish = s2;
			}
			if (verbPharseStartQues == false && vbNeed == false
					&& expletive == false) {
				splitWord(phrases.get(4));
				keyworld = s1;
				splitWord(phrases.get(3));
				nameFish = s2;
			}
		}
		// question 20: hướng dẫn nuôi cá A?
		if (kindQues.equals("questionWh20 ")) {
			splitWord(phrases.get(3));
			keyworld = s1;
			splitWord(phrases.get(5));
			nameFish = s2;
		} 
	}

	private int count(int a) {
		a = 0;
		ArrayList<Boolean> s = new ArrayList<>();
		s.add(dtThe);
		s.add(inCan);
		s.add(verbPharseStartQues);
		s.add(expletive);
		for (int i = 0; i < s.size(); i++) {
			if (s.get(i) == false)
				a++;
		}
		return a;
	}
}
